﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OOAD_Project_Editor.Foundation;
using OOAD_Project_Engine.Foundation.Interfaces;
using Microsoft.Xna.Framework;

namespace OOAD_Project_Editor.Domain
{
    class ActionHandler : GameComponent, IActionHandler
    {
        ISelection Selection { get; set; }
        List<IManageSceneAction> actions;
        public event EventHandler<SelectEntityEventArgs> OnEntitySelectedEvent;

        public ActionHandler(Game game)
            : base(game)
        {
            actions = new List<IManageSceneAction>();
        }

        public override void Initialize()
        {
            base.Initialize();
            if (Selection != null && Game.Components.Contains(Selection))
                Game.Components.Remove(Selection);
            Selection = new SelectionBound(Game);
            Game.Components.Add(Selection);
        }

        public IManageSceneAction AddAction(EActionType actionType, params object[] param)
        {
            IManageSceneAction action = ActionFactory.CreateAction(actionType, param);
            actions.Add(action);
            action.DoAction();
            return action;
        }

        private void onEntitySelected(EventHandler<SelectEntityEventArgs> _event, IEntity entity)
        {
            if (_event != null)
            {
                SelectEntityEventArgs e = new SelectEntityEventArgs(entity);
                _event(this, e);
            }
        }

        public IEntity SelectEntity
        {
            get
            {
                return Selection.Target;
            }
            set
            {
                if (value != null)
                    onEntitySelected(OnEntitySelectedEvent, value);
                //if (Selection != null)
                Selection.Target = value;
            }
        }

        #region UI

        public bool EnableCopy()
        {
            return SelectEntity != null;
        }

        public bool EnableCut()
        {
            return SelectEntity != null;
        }

        public bool EnablePaste()
        {
            bool enable = !EntityClipboard.GetInstance().IsEmpty();
            return enable;
        }

        #endregion

        public void EntityDragStart()
        {
            if (Selection != null && Selection.Target != null)
                Selection.DragStarted();
        }

        public void EntityDragEnd()
        {
            if (Selection != null && Selection.Target != null)
            {
                Selection.DragEnded();
                onEntitySelected(OnEntitySelectedEvent, Selection.Target);
            }

        }

        public void EntityDraging()
        {
            if (Selection != null && Selection.Target != null)
                Selection.Draging();
        }

    }
}
